#include "StdAfx.h"
#include "ExtListHeadCtrl.h"

#ifdef _DEBUG
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)  
#endif

CExtListHeadCtrl::CExtListHeadCtrl(void)
{
	m_bEnableBorder = TRUE;
	m_bEnableDivline = FALSE;
	m_clrHeadText = RGB(255, 255, 255);
	m_clrHeadBk = RGB(63, 69, 85);					
	m_clrHeadDivLine = RGB(74, 80, 99);	
	m_clrHeadBorder = RGB(74, 80, 99);
	m_nHeadBorderPixel = 1;
	m_nHeadDivlinePixel = 2;
	m_nHeaderHeight = 26;
	m_uUnallowDbClickCount = 0;
	m_uLockCount = 0;

	m_fontHeadText.CreateFont(20, 0, 0, 0, FW_NORMAL,FALSE, FALSE, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS,  _T("微软雅黑"));
}
CExtListHeadCtrl::~CExtListHeadCtrl(void)
{
}

BEGIN_MESSAGE_MAP(CExtListHeadCtrl, CHeaderCtrl)
	ON_WM_PAINT()
	ON_MESSAGE(HDM_LAYOUT, OnHeadLayOut)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()


void CExtListHeadCtrl::SetHeaderHeight(int nHeaderHeight)
{
	m_nHeaderHeight = nHeaderHeight;
	Invalidate();
}
void CExtListHeadCtrl::SetHeaderBkColor(COLORREF clrHeadBk)
{
	m_clrHeadBk = clrHeadBk;
	Invalidate();
}
void CExtListHeadCtrl::SetHeaderTextColor(COLORREF clrHeadText)
{
	m_clrHeadText = clrHeadText;
	Invalidate();
}
void CExtListHeadCtrl::SetHeaderFont(int nFontHeight, BOOL bBold/* = FALSE*/)	
{
	m_fontHeadText.DeleteObject();
	m_fontHeadText.CreateFont(nFontHeight, 0, 0, 0, bBold ? FW_BOLD : FW_NORMAL,FALSE, FALSE, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
		CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS,  _T("微软雅黑"));
	Invalidate();
}

LRESULT CExtListHeadCtrl::OnHeadLayOut(WPARAM wParam, LPARAM lParam)
{
	LRESULT lResult = CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);
	//设置头高度
	HD_LAYOUT& hdl = *(HD_LAYOUT *)lParam;
	RECT* pRc = hdl.prc;
	WINDOWPOS* pWinPos = hdl.pwpos;
	pWinPos->cy = m_nHeaderHeight;
	pRc->top = m_nHeaderHeight;
	return lResult;
}
BOOL CExtListHeadCtrl::OnChildNotify(UINT uMessage, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	//变量定义
	NMHEADER* pNMHeader = (NMHEADER *)lParam;
	
	//如拖动消息，则禁止拖动
 	if (pNMHeader->hdr.code == HDN_BEGINTRACKA || pNMHeader->hdr.code == HDN_BEGINTRACKW || pNMHeader->hdr.code == HDN_BEGINDRAG)		 
 	{
 		//m_uLockCount是代表不让前几个不拖动
 		if (pNMHeader->iItem < (int)m_uLockCount)
 		{
 			*pResult = TRUE;
 			return TRUE;
 		}
 	}
 	else if (pNMHeader->hdr.code == HDN_DIVIDERDBLCLICKW || pNMHeader->hdr.code == HDN_DIVIDERDBLCLICKA)
 	{
 		//m_uUnallowDbClickCount是代表不让双击隐藏个数
 		if (pNMHeader->iItem < (int)m_uUnallowDbClickCount)
 		{
 			*pResult = TRUE;
 			return TRUE;
 		}
 	}
 
 	if (pNMHeader->hdr.code == HDN_ENDDRAG || pNMHeader->hdr.code == HDN_ENDTRACK)
 	{
 		//::SendMessage(m_ListhWnd, WM_MSG_HEADMOVE, 0, 0);
 	}
	return  __super::OnChildNotify(uMessage, wParam, lParam, pResult);
}

void CExtListHeadCtrl::SetLockCount(UINT uLockCount)
{
	m_uLockCount = uLockCount;
}
void CExtListHeadCtrl::SetUnallowDbClickCount( UINT uCount )
{
	m_uUnallowDbClickCount = uCount;
}

void CExtListHeadCtrl::OnPaint()
{
	CPaintDC dc(this); 
	CRect	rcHead, rcItem;
	CDC		MemDC;
	CBitmap	BmpMem;
	HDITEM	HDItem;
	char	szBuffer[64] = {0};
	CPen	pen(PS_SOLID, m_nHeadDivlinePixel, m_clrHeadDivLine);


	GetClientRect(&rcHead);
	//内存DC
	MemDC.CreateCompatibleDC(&dc);
	BmpMem.CreateCompatibleBitmap(&dc, rcHead.Width(), rcHead.Height());
	MemDC.SelectObject(&BmpMem);
	//设置DC
	MemDC.SetBkMode(TRANSPARENT);
	MemDC.SetTextColor(m_clrHeadText);
	MemDC.SelectObject(&m_fontHeadText);

	//画背景	
	MemDC.FillSolidRect(rcHead, m_clrHeadBk);

	MemDC.SelectObject(&pen);
	//画子项
	for (int i = 0; i < GetItemCount(); i++)
	{
		HDItem.mask = HDI_TEXT;
		HDItem.pszText = szBuffer;
		HDItem.cchTextMax = 64;
		
		GetItem(i, &HDItem);
		GetItemRect(i, &rcItem);

		//分隔线
		MemDC.MoveTo(rcItem.right, rcItem.top);
		MemDC.LineTo(rcItem.right, rcItem.bottom);

		//画文本
		MemDC.DrawText(szBuffer, lstrlen(szBuffer), &rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
	}

	//画边框
	if (m_bEnableBorder)
	{
		DrawHeaderBorder(&MemDC);
	}

	dc.BitBlt(rcHead.left, rcHead.top, rcHead.Width(), rcHead.Height(), &MemDC, 0, 0, SRCCOPY);

	MemDC.DeleteDC();
	BmpMem.DeleteObject();
}

void CExtListHeadCtrl::SetHeaderBorder(BOOL bEnableBorder, COLORREF clrHeadBorder, UINT nPixel)
{
	m_bEnableBorder = bEnableBorder;
	m_clrHeadBorder = clrHeadBorder;
	m_nHeadBorderPixel = nPixel;

	if (m_bEnableBorder){		
		Invalidate();
	}
}
void CExtListHeadCtrl::DrawHeaderBorder(CDC* pDC)
{
	CRect	rcClient;
	CPoint	ptStart, ptEnd;
	CPen	pen(PS_SOLID, m_nHeadBorderPixel, m_clrHeadBorder);
	GetClientRect(&rcClient);
	//rcClient.right -=9;
	pDC->SelectObject(&pen);
	pDC->SelectObject((HBRUSH)::GetStockObject(NULL_BRUSH));
	pDC->Rectangle(rcClient);
}

void CExtListHeadCtrl::SetHeaderDivline( BOOL bEnableDivline, COLORREF clrHeadDivline, UINT nPixel )
{
	m_bEnableBorder = bEnableDivline;
	m_clrHeadDivLine = clrHeadDivline;
	m_nHeadDivlinePixel = nPixel;

	if (m_bEnableBorder){
		Invalidate();
	}
}

BOOL CExtListHeadCtrl::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

